import { routes } from "@/router";
import { getUsername, getDomain, getRoles } from "@/utils/auth";
// import { Notify } from "vant";

const getRoutesTips = () => routes.filter(v => v.pcurl)
  .map(v => ({ ...v, component: undefined }));

const recursiveRoutes = (rolesData, routesTipsData) => {
  for (let i = 0, l = rolesData.length; i < l; i++) {
    let item = rolesData[i];
    let { children, hash } = item

    let targetItem = routesTipsData.find(v => v.pcurl === hash)
    if (targetItem) targetItem.visible = true

    if (Array.isArray(children) && children.length) {
      recursiveRoutes(children, routesTipsData)
    }
  }
}

const routesTips = getRoutesTips()

recursiveRoutes(getRoles(), routesTips)

/**
 * tabbar-每个目录的待处理数量
 */
const state = {
  webSocket: null,
  wsInterval: null,
  routesTips
}

const getters = {
  checkTipsTotal: state => {
    let total = 0;

    state.routesTips
      .filter(({ visible, path }) => visible && path.indexOf('/check/') === 0)
      .forEach(({ count }) => {
        Number.isInteger(count) && (total += count)
      });

    return total;
  }
}

const mutations = {
  setRoutesTips: (state, routesTips) => {
    state.routesTips = routesTips
  },
  setWebSocket: (state) => {
    const userID = getUsername()
    let domain = getDomain().replace('http://', '').split(':');
    domain[1] = domain[1] * 1 + 1;
    domain = domain.join(':');

    state.webSocket && state.webSocket.close()
    state.webSocket = new WebSocket(`ws://${domain}/${userID}`);
  },
  setWsInterval: (state, wsInterval) => {
    state.wsInterval && clearInterval(state.wsInterval)
    state.wsInterval = wsInterval
  },
  removeWebSocket: (state) => {
    state.webSocket && state.webSocket.close()
    state.webSocket = null
  }
}

const actions = {
  setRoutesTips({ commit }, routesTips) {
    commit('setRoutesTips', routesTips)
  },
  setWebSocket({ dispatch, commit, state }) {
    const routesTips = getRoutesTips()
    recursiveRoutes(getRoles(), routesTips)
    dispatch('setRoutesTips', routesTips)

    commit('setWebSocket')

    state.webSocket.onopen = () => {
      dispatch('setWsInterval', setInterval(() => {
        dispatch('setWebSocket')
      }, 60 * 1000))
    }

    state.webSocket.onmessage = ({ data }) => {
      const arrData = JSON.parse(data)

      const routesTips = state.routesTips.map(v => {
        let targetItem = arrData.find(({ code }) => code === v.pcurl)
        if (targetItem) {
          return { ...v, count: targetItem.total }
        }
        return v
      });

      dispatch('setRoutesTips', routesTips)

      dispatch('setWsInterval', setInterval(() => {
        dispatch('setWebSocket')
      }, 60 * 1000))
    }

    state.webSocket.onerror = () => {
      // Notify({ type: 'danger', message: '待处理计数服务连接失败,请尝试重登' });
    }
  },
  setWsInterval({ commit }, wsInterval) {
    commit('setWsInterval', wsInterval)
  },
  removeWebSocket({ dispatch, commit }) {
    commit('removeWebSocket')
    dispatch('setWsInterval', null)
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}